package me.sdevil507.supports.word;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import javax.servlet.ServletOutputStream;
import java.io.ByteArrayInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @Author hp
 * @Date 2021/7/20 15:01
 * @Description WordUtil
 * @Version 1.0
 */
public class WordUtil {

    /**
     * 富文本 导出word方法
     * @param request
     * @param response
     * @param html
     * @param title
     */
    public static void exportWords(String html, ZipOutputStream zipOut) {
        POIFSFileSystem poifs = null;
        ByteArrayInputStream bais = null;
        try {
            //设置编码
            byte b[] = html.getBytes("utf-8");
            bais = new ByteArrayInputStream(b);
            poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
            poifs.writeFilesystem(zipOut);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
                if (poifs != null) {
                    poifs.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
